home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the 3D Game Programming Gurus / gurus.iso / DirectX / dx9sdkcp.exe / SDK (C++) / Bin / DXUtils / Visual Studio 6.0 Wizards / Source Code / Template / dsutil.h < prev    next >
Encoding:
C/C++ Source or Header  |  2002-11-12  |  5.9 KB  |  172 lines

  1. //-----------------------------------------------------------------------------
  2. // File: DSUtil.h
  3. //
  4. // Desc: 
  5. //-----------------------------------------------------------------------------
  6. #ifndef DSUTIL_H
  7. #define DSUTIL_H
  8.  
  9. #include <windows.h>
  10. #include <mmsystem.h>
  11. #include <mmreg.h>
  12. #include <dsound.h>
  13.  
  14.  
  15.  
  16.  
  17. //-----------------------------------------------------------------------------
  18. // Classes used by this header
  19. //-----------------------------------------------------------------------------
  20. class CSoundManager;
  21. class CSound;
  22. class CStreamingSound;
  23. class CWaveFile;
  24.  
  25.  
  26.  
  27.  
  28. //-----------------------------------------------------------------------------
  29. // Typing macros 
  30. //-----------------------------------------------------------------------------
  31. #define WAVEFILE_READ   1
  32. #define WAVEFILE_WRITE  2
  33.  
  34. #define DSUtil_StopSound(s)         { if(s) s->Stop(); }
  35. #define DSUtil_PlaySound(s)         { if(s) s->Play( 0, 0 ); }
  36. #define DSUtil_PlaySoundLooping(s)  { if(s) s->Play( 0, DSBPLAY_LOOPING ); }
  37.  
  38.  
  39.  
  40.  
  41. //-----------------------------------------------------------------------------
  42. // Name: class CSoundManager
  43. // Desc: 
  44. //-----------------------------------------------------------------------------
  45. class CSoundManager
  46. {
  47. protected:
  48.     LPDIRECTSOUND8 m_pDS;
  49.  
  50. public:
  51.     CSoundManager();
  52.     ~CSoundManager();
  53.  
  54.     HRESULT Initialize( HWND hWnd, DWORD dwCoopLevel );
  55.     inline  LPDIRECTSOUND8 GetDirectSound() { return m_pDS; }
  56.     HRESULT SetPrimaryBufferFormat( DWORD dwPrimaryChannels, DWORD dwPrimaryFreq, DWORD dwPrimaryBitRate );
  57.     HRESULT Get3DListenerInterface( LPDIRECTSOUND3DLISTENER* ppDSListener );
  58.  
  59.     HRESULT Create( CSound** ppSound, LPTSTR strWaveFileName, DWORD dwCreationFlags = 0, GUID guid3DAlgorithm = GUID_NULL, DWORD dwNumBuffers = 1 );
  60.     HRESULT CreateFromMemory( CSound** ppSound, BYTE* pbData, ULONG ulDataSize, LPWAVEFORMATEX pwfx, DWORD dwCreationFlags = 0, GUID guid3DAlgorithm = GUID_NULL, DWORD dwNumBuffers = 1 );
  61.     HRESULT CreateStreaming( CStreamingSound** ppStreamingSound, LPTSTR strWaveFileName, DWORD dwCreationFlags, GUID guid3DAlgorithm, DWORD dwNotifyCount, DWORD dwNotifySize, HANDLE hNotifyEvent );
  62. };
  63.  
  64.  
  65.  
  66.  
  67. //-----------------------------------------------------------------------------
  68. // Name: class CSound
  69. // Desc: Encapsulates functionality of a DirectSound buffer.
  70. //-----------------------------------------------------------------------------
  71. class CSound
  72. {
  73. protected:
  74.     LPDIRECTSOUNDBUFFER* m_apDSBuffer;
  75.     DWORD                m_dwDSBufferSize;
  76.     CWaveFile*           m_pWaveFile;
  77.     DWORD                m_dwNumBuffers;
  78.     DWORD                m_dwCreationFlags;
  79.  
  80.     HRESULT RestoreBuffer( LPDIRECTSOUNDBUFFER pDSB, BOOL* pbWasRestored );
  81.  
  82. public:
  83.     CSound( LPDIRECTSOUNDBUFFER* apDSBuffer, DWORD dwDSBufferSize, DWORD dwNumBuffers, CWaveFile* pWaveFile, DWORD dwCreationFlags );
  84.     virtual ~CSound();
  85.  
  86.     HRESULT Get3DBufferInterface( DWORD dwIndex, LPDIRECTSOUND3DBUFFER* ppDS3DBuffer );
  87.     HRESULT FillBufferWithSound( LPDIRECTSOUNDBUFFER pDSB, BOOL bRepeatWavIfBufferLarger );
  88.     LPDIRECTSOUNDBUFFER GetFreeBuffer();
  89.     LPDIRECTSOUNDBUFFER GetBuffer( DWORD dwIndex );
  90.  
  91.     HRESULT Play( DWORD dwPriority = 0, DWORD dwFlags = 0, LONG lVolume = 0, LONG lFrequency = -1, LONG lPan = 0 );
  92.     HRESULT Play3D( LPDS3DBUFFER p3DBuffer, DWORD dwPriority = 0, DWORD dwFlags = 0, LONG lFrequency = 0 );
  93.     HRESULT Stop();
  94.     HRESULT Reset();
  95.     BOOL    IsSoundPlaying();
  96. };
  97.  
  98.  
  99.  
  100.  
  101. //-----------------------------------------------------------------------------
  102. // Name: class CStreamingSound
  103. // Desc: Encapsulates functionality to play a wave file with DirectSound.  
  104. //       The Create() method loads a chunk of wave file into the buffer, 
  105. //       and as sound plays more is written to the buffer by calling 
  106. //       HandleWaveStreamNotification() whenever hNotifyEvent is signaled.
  107. //-----------------------------------------------------------------------------
  108. class CStreamingSound : public CSound
  109. {
  110. protected:
  111.     DWORD m_dwLastPlayPos;
  112.     DWORD m_dwPlayProgress;
  113.     DWORD m_dwNotifySize;
  114.     DWORD m_dwNextWriteOffset;
  115.     BOOL  m_bFillNextNotificationWithSilence;
  116.  
  117. public:
  118.     CStreamingSound( LPDIRECTSOUNDBUFFER pDSBuffer, DWORD dwDSBufferSize, CWaveFile* pWaveFile, DWORD dwNotifySize );
  119.     ~CStreamingSound();
  120.  
  121.     HRESULT HandleWaveStreamNotification( BOOL bLoopedPlay );
  122.     HRESULT Reset();
  123. };
  124.  
  125.  
  126.  
  127.  
  128. //-----------------------------------------------------------------------------
  129. // Name: class CWaveFile
  130. // Desc: Encapsulates reading or writing sound data to or from a wave file
  131. //-----------------------------------------------------------------------------
  132. class CWaveFile
  133. {
  134. public:
  135.     WAVEFORMATEX* m_pwfx;        // Pointer to WAVEFORMATEX structure
  136.     HMMIO         m_hmmio;       // MM I/O handle for the WAVE
  137.     MMCKINFO      m_ck;          // Multimedia RIFF chunk
  138.     MMCKINFO      m_ckRiff;      // Use in opening a WAVE file
  139.     DWORD         m_dwSize;      // The size of the wave file
  140.     MMIOINFO      m_mmioinfoOut;
  141.     DWORD         m_dwFlags;
  142.     BOOL          m_bIsReadingFromMemory;
  143.     BYTE*         m_pbData;
  144.     BYTE*         m_pbDataCur;
  145.     ULONG         m_ulDataSize;
  146.     CHAR*         m_pResourceBuffer;
  147.  
  148. protected:
  149.     HRESULT ReadMMIO();
  150.     HRESULT WriteMMIO( WAVEFORMATEX *pwfxDest );
  151.  
  152. public:
  153.     CWaveFile();
  154.     ~CWaveFile();
  155.  
  156.     HRESULT Open( LPTSTR strFileName, WAVEFORMATEX* pwfx, DWORD dwFlags );
  157.     HRESULT OpenFromMemory( BYTE* pbData, ULONG ulDataSize, WAVEFORMATEX* pwfx, DWORD dwFlags );
  158.     HRESULT Close();
  159.  
  160.     HRESULT Read( BYTE* pBuffer, DWORD dwSizeToRead, DWORD* pdwSizeRead );
  161.     HRESULT Write( UINT nSizeToWrite, BYTE* pbData, UINT* pnSizeWrote );
  162.  
  163.     DWORD   GetSize();
  164.     HRESULT ResetFile();
  165.     WAVEFORMATEX* GetFormat() { return m_pwfx; };
  166. };
  167.  
  168.  
  169.  
  170.  
  171. #endif // DSUTIL_H
  172.